package handler

import (
	"common"
	"common/biz"
	"common/logs"
	"context"
	"core/repo"
	"core/service"
	"encoding/json"
	"framework/remote"
	"hall/models/request"
	"hall/models/response"
	"strconv"
)

type WarehouseHandler struct {
	warehouseService *service.WarehouseService
	userService      *service.UserService
	base             *BaseHandler
}

func (w *WarehouseHandler) WarehouseList(session *remote.Session, msg []byte) any {
	logs.Info("[warehouseHandler] WarehouseList msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[warehouseHandler] WarehouseList err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := w.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.WarehouseScreen
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[warehouseHandler]用户%d WarehouseList err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	material, card, equip := w.warehouseService.WarehouseList(ctx, uint(uid), &req)
	return common.S(response.WarehouseRes{
		MaterialData: material,
		CardData:     card,
		EquipData:    equip,
	})
}

func (w *WarehouseHandler) WarehouseInfo(session *remote.Session, msg []byte) any {
	logs.Info("[warehouseHandler] WarehouseInfo msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[warehouseHandler] WarehouseInfo err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := w.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.WarehouseGetInfo
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[warehouseHandler]用户%d WarehouseInfo err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	data := w.warehouseService.GetInfo(ctx, uint(uid), &req)
	return common.S(data)
}

func (w *WarehouseHandler) WarehouseMaterial(session *remote.Session, msg []byte) any {
	logs.Info("[warehouseHandler] WarehouseInfo msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[warehouseHandler] WarehouseInfo err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := w.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CommonTypeID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[warehouseHandler]用户%d WarehouseInfo err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	data := w.warehouseService.WarehouseMaterial(ctx, uint(uid), req.TypeID)
	return common.S(data)
}

func (w *WarehouseHandler) WarehouseOpenCardPackage(session *remote.Session, msg []byte) any {
	logs.Info("[warehouseHandler] WarehouseOpenCardPackage msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[warehouseHandler] WarehouseOpenCardPackage err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := w.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CommonID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[warehouseHandler]用户%d WarehouseOpenCardPackage err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	cardPackage, types, donate, err := w.warehouseService.OpenCardPackage(ctx, uint(uid), req.ID)
	if err != nil {
		return common.F(err)
	}
	return common.S(map[string]any{
		"cardPackage": cardPackage,
		"donateType":  types,
		"donate":      donate,
	})
}

func (w *WarehouseHandler) WarehouseCardNext(session *remote.Session, msg []byte) any {
	logs.Info("[warehouseHandler] WarehouseCardNext msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[warehouseHandler] WarehouseCardNext err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := w.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CommonID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[warehouseHandler]用户%d WarehouseCardNext err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	data := w.warehouseService.WarehouseCardNext(ctx, uint(uid), req.ID)
	return common.S(data)
}

func (w *WarehouseHandler) WarehouseCardUpgrade(session *remote.Session, msg []byte) any {
	logs.Info("[warehouseHandler] WarehouseCardUpgrade msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[warehouseHandler] WarehouseCardUpgrade err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := w.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CommonID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[warehouseHandler]用户%d WarehouseCardUpgrade err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	err := w.warehouseService.WarehouseCardUpgrade(ctx, uint(uid), req.ID)
	if err != nil {
		return common.F(err)
	}
	return common.S(nil)
}

func (w *WarehouseHandler) WarehouseGoldTransfer(session *remote.Session, msg []byte) any {
	logs.Info("[warehouseHandler] WarehouseCardUpgrade msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[warehouseHandler] WarehouseGoldTransfer err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := w.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.WarehouseGoldTransfer
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[warehouseHandler]用户%d WarehouseGoldTransfer err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	err := w.warehouseService.WarehouseGoldTransfer(ctx, uint(uid), &req)
	if err != nil {
		return common.F(err)
	}
	return common.S(nil)
}

func (w *WarehouseHandler) WarehouseExtract(session *remote.Session, msg []byte) any {
	logs.Info("[warehouseHandler] WarehouseExtract msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[warehouseHandler] WarehouseExtract err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := w.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.WarehouseExtract
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[warehouseHandler]用户%d WarehouseExtract err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	err := w.warehouseService.WarehouseExtract(ctx, uint(uid), &req)
	if err != nil {
		return common.F(err)
	}
	return common.S(nil)
}

func NewWarehouseHandler(r *repo.Manager) *WarehouseHandler {
	return &WarehouseHandler{
		warehouseService: service.NewWarehouseService(r),
		userService:      service.NewUserService(r),
		base:             NewBaseHandler(r),
	}
}
